<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加</title>
<link rel="stylesheet" href="${ctx }/resources/layui/css/layui.css" media="all" />
<link rel="stylesheet" href="${ctx }/resources/css/public.css" media="all" />
<link rel="stylesheet" href="${ctx }/resources/zTree/css/metroStyle/metroStyle.css" type="text/css">
<link rel="stylesheet" type="text/css" href="${ctx }/resources/plugin/css/index.css">
</head>
<body class="childrenBody">
	<form class="layui-form" action="#" id="frm" method="post" lay-filter="frm">
		<div class="layui-form-item">
			<div class="layui-inline">
				<label class="layui-form-label">选择供应商</label>
				<div class="layui-input-inline">
					<div id="providerid" name="providerid" class="layui-form-select select-tree"></div>
				</div>
			</div>
		</div>
		
	<div class="layui-form-item">
		<div class="layui-inline">
			<label class="layui-form-label">商品名称</label>
			<div class="layui-input-inline">
				<input type="text" name="goodsname" id="goodsname" lay-verify="required"
					autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">产地</label>
			<div class="layui-input-inline">
				<input type="text" name="produceplace" id="produceplace"
					autocomplete="off" class="layui-input">
			</div>
		</div>
	</div>
	<div class="layui-form-item">
		<div class="layui-inline">
			<label class="layui-form-label">规格</label>
			<div class="layui-input-inline">
				<input type="text" name="size" id="size" lay-verify="required"
					autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">包装</label>
			<div class="layui-input-inline">
				<input type="text" name="goodspackage" id="goodspackage"
					autocomplete="off" class="layui-input">
			</div>
		</div>
	</div>
	<div class="layui-form-item">
		<div class="layui-inline">
			<label class="layui-form-label">生产批号</label>
			<div class="layui-input-inline">
				<input type="text" name="productcode" id="productcode" lay-verify="required"
					autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">批准文号</label>
			<div class="layui-input-inline">
				<input type="text" name="promitcode" id="promitcode"
					autocomplete="off" class="layui-input">
			</div>
		</div>
	</div>
	<div class="layui-form-item">
		<div class="layui-inline">
			<label class="layui-form-label">销售参考价格</label>
			<div class="layui-input-inline">
				<input type="text" name="price" id="price" lay-verify="required"
					autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">是否可用</label>
			<div class="layui-input-inline">
				<input type="radio" name="available" value="1" title="是" checked="">
				<input type="radio" name="available" value="0" title="否"> 
			</div>
		</div>
	</div>
	<div class="layui-form-item">
		<div class="layui-inline">
			<label class="layui-form-label">库存</label>
			<div class="layui-input-inline">
				<input type="text" name="number" id="number" lay-verify="required"
					autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-inline">
			<label class="layui-form-label">预警库存</label>
			<div class="layui-input-inline">
				<input type="text" name="dangernum" id="dangernum" lay-verify="required"
					autocomplete="off" class="layui-input">
			</div>
		</div>
	</div>
		
	<div class="layui-form-item layui-form-text">
		<label class="layui-form-label">商品描述</label>
		<div class="layui-input-block">
			<textarea placeholder="请输入内容" name="description" class="layui-textarea"></textarea>
		</div>
	</div>
	
	<div class="layui-form-item layui-form-text">
		<label class="layui-form-label">商品图片</label>
		<div class="layui-upload">
			<img class="layui-upload-img" width="100px" height="80px" id="goodsimg_img">
			<button type="button" class="layui-btn" id="goodspath">上传图片</button>
			<span id="goodsText"></span>
			<input type="hidden" name="goodsimg" id="goodsimg">
		</div>
	</div>
		
		<div class="layui-form-item">
			<div class="layui-input-block" style="text-align: center;">
				<!--lay-submit标记当按钮是一个提交按钮   -->
				<a href="javascript:void(o)" class="layui-btn" lay-submit=""  lay-filter="goodsSubmit">提交</a>
				<button type="reset" class="layui-btn layui-btn-primary">重置</button>
			</div>
		</div>
	</form>
</body>
<script type="text/javascript" src="${ctx }/resources/layui/layui.js"></script>
<script type="text/javascript" src="${ctx }/resources/zTree/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="${ctx }/resources/zTree/js/jquery.ztree.core.js"></script>
<script type="text/javascript" src="${ctx }/resources/plugin/js/selectTree.js"></script>
<script type="text/javascript">
layui.use([ 'form','jquery','layer','upload' ], function() {
	var form=layui.form;//得到form对象
	var $=layui.jquery;
	var upload = layui.upload;
	var layer = parent.layer === undefined ? layui.layer : parent.layer;
	//添加提交事件
	form.on("submit(goodsSubmit)",function(data){
		var params=$("#frm").serialize();
		$.post("${ctx}/goods/addGoods.action?"+params,function(obj){
			layer.msg(obj.msg);
			parent.tableIns.reload();//刷新表格
			parent.parent.left.initTree();//刷新左边的树
			//关闭窗口
			var index=layer.getFrameIndex(window.name);
			layer.close(index);
		}) 
	});
	
	//普通图片上传
	var uploadInst = upload
			.render({
				elem : '#goodspath',
				url : '${ctx}/upload/uploadFile.action',
				accept:'images',
				acceptMime:'image/*',
				field:'mf',
				before : function(obj) {
					//预读本地文件示例，不支持ie8
					obj.preview(function(index, file,
							result) {
						$('#goodsimg_img').attr('src', result); //图片链接（base64）
					});
				},
				done : function(res) {
					//如果上传失败
					if (res.code > 0) {
						return layer.msg('上传失败');
					}
					//上传成功
					$('#goodsimg_img').attr('src', res.data.src); //图片链接（base64）
					$('#goodsimg').val(res.data.src);
				},
				error : function() {
					//演示失败状态，并实现重传
					var goodsText = $('#goodsText');
					goodsText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs goods-reload">重试</a>');
					goodsText.find('.goods-reload').on(
							'click', function() {
								uploadInst.upload();
							});
				}
			});
	
});	
	$(document).ready(function () {
       initProviderTree();
    });
	
	function initProviderTree() {
		//加载可用商品
		$.post("${ctx}/goods/loadProviderTree.action?available=1",function(zNodes){
			initSelectTree("providerid",zNodes, false);
	        $(".layui-nav-item a").bind("click", function () {
	            if (!$(this).parent().hasClass("layui-nav-itemed") && !$(this).parent().parent().hasClass("layui-nav-child")) {
	                $(".layui-nav-tree").find(".layui-nav-itemed").removeClass("layui-nav-itemed")
	            }
	        })
		})
	}
	

</script>
</html>